import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/home_controller.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(
      init: HomeController(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('首页'),
            actions: [
              IconButton(
                icon: const Icon(Icons.person),
                onPressed: () => Get.toNamed('/profile'),
              ),
              IconButton(
                icon: const Icon(Icons.settings),
                onPressed: () => Get.toNamed('/settings'),
              ),
            ],
          ),
          body: SafeArea(
            child: Column(
              children: [
                // 顶部卡片
                Container(
                  margin: EdgeInsets.all(16.w),
                  padding: EdgeInsets.all(20.w),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        Theme.of(context).colorScheme.primary,
                        Theme.of(context).colorScheme.secondary,
                      ],
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                    ),
                    borderRadius: BorderRadius.circular(12.r),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.1),
                        blurRadius: 10.r,
                        offset: Offset(0, 5.h),
                      ),
                    ],
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '欢迎使用 Flutter Base',
                        style: TextStyle(
                          fontSize: 20.sp,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                      SizedBox(height: 8.h),
                      Text(
                        '这是一个完整的 Flutter 项目基础框架',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white.withOpacity(0.9),
                        ),
                      ),
                    ],
                  ),
                ),
                
                // 功能网格
                Expanded(
                  child: GridView.count(
                    crossAxisCount: 2,
                    padding: EdgeInsets.all(16.w),
                    mainAxisSpacing: 16.h,
                    crossAxisSpacing: 16.w,
                    children: [
                      _buildFeatureCard(
                        context,
                        icon: Icons.network_check,
                        title: '网络请求',
                        subtitle: '查看网络请求示例',
                        color: Colors.blue,
                        onTap: () => controller.testNetworkRequest(),
                      ),
                      _buildFeatureCard(
                        context,
                        icon: Icons.storage,
                        title: '本地存储',
                        subtitle: '测试本地存储功能',
                        color: Colors.green,
                        onTap: () => controller.testLocalStorage(),
                      ),
                      _buildFeatureCard(
                        context,
                        icon: Icons.color_lens,
                        title: '主题切换',
                        subtitle: '切换应用主题',
                        color: Colors.purple,
                        onTap: () => controller.toggleTheme(),
                      ),
                      _buildFeatureCard(
                        context,
                        icon: Icons.list,
                        title: '列表页面',
                        subtitle: '查看列表示例',
                        color: Colors.red,
                        onTap: () => Get.toNamed('/list'),
                      ),
                      _buildFeatureCard(
                        context,
                        icon: Icons.tab,
                        title: '底部Tab',
                        subtitle: '底部导航示例',
                        color: Colors.teal,
                        onTap: () => Get.toNamed('/main-tab'),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildFeatureCard(
    BuildContext context, {
    required IconData icon,
    required String title,
    required String subtitle,
    required Color color,
    required VoidCallback onTap,
  }) {
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(12.r),
        child: Padding(
          padding: EdgeInsets.all(16.w),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: 60.w,
                height: 60.w,
                decoration: BoxDecoration(
                  color: color.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(30.r),
                ),
                child: Icon(
                  icon,
                  size: 30.w,
                  color: color,
                ),
              ),
              SizedBox(height: 12.h),
              Text(
                title,
                style: TextStyle(
                  fontSize: 16.sp,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),
              SizedBox(height: 4.h),
              Text(
                subtitle,
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colors.grey[600],
                ),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      ),
    );
  }
}